package com.wing.order.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.utils.JsonResult;
import com.wing.order.dao.OrderReceiveLoadDetailDao;
import com.wing.order.model.entity.OrderReceiveLoadDetail;
import com.wing.order.model.request.OrderReceiveLoadDetailForm;
import com.wing.order.model.response.OrderReceiveLoadDetailVO;
import com.wing.user.feign.UserClient;
import com.wing.user.model.response.DepartmentVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 上货明细-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class OrderReceiveLoadDetailServiceImpl extends ServiceImpl<OrderReceiveLoadDetailDao, OrderReceiveLoadDetail> implements OrderReceiveLoadDetailService {
    @Autowired
    private UserClient userClient;
    @Override
    public int add(OrderReceiveLoadDetailForm orderReceiveLoadDetailForm) {

        //校验

        OrderReceiveLoadDetail orderReceiveLoadDetail =new OrderReceiveLoadDetail();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(OrderReceiveLoadDetail.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(orderReceiveLoadDetailForm, orderReceiveLoadDetail, copyOptions);

        return baseMapper.insert(orderReceiveLoadDetail);
    }

    @Override
    public List<OrderReceiveLoadDetailVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<OrderReceiveLoadDetailVO> pageList4app(Page page, Map<String, Object> params) {
        List<OrderReceiveLoadDetailVO> list = baseMapper.list4app(page, params);
        for(OrderReceiveLoadDetailVO item : list){
            JsonResult<DepartmentVO> jsonResult = userClient.getDepartmentVOById(item.getDepartmentId());
            if(null != jsonResult && null != jsonResult.getData()) {
                item.setCostCenter(jsonResult.getData().getCostCenter());
            }
        }
        page.setRecords(list);
        return page;
    }

    @Override
    public OrderReceiveLoadDetailVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }
}
